package com.mystnihon.freeremote.loader.torrent;

import java.io.InputStream;

import android.content.Context;
import android.net.Uri;
import android.util.Log;
import ca.benow.transmission.TransmissionClient;
import ca.benow.transmission.model.AddedTorrentInfo;

import com.octo.android.robospice.request.SpiceRequest;

public class AddTorrentRequest extends SpiceRequest<AddedTorrentInfo> {

    private static final String TAG = AddTorrentRequest.class.getSimpleName();
    private TransmissionClient mClient;
    private Uri mUri;
    private Context mContext;

    public AddTorrentRequest(Context context, TransmissionClient client, Uri uri) {
        super(AddedTorrentInfo.class);
        mClient = client;
        mContext = context;
        mUri = uri;

    }

    @Override
    public AddedTorrentInfo loadDataFromNetwork() throws Exception {
        Log.i(TAG, "Will upload : " + mUri.toString());
        if (mUri.getScheme().equals("file")) {
            InputStream in = mContext.getContentResolver().openInputStream(mUri);
            AddedTorrentInfo data = mClient.addTorrent(null, in, false, -1, -1, null, null, null, null, null);
            in.close();
            return (data);

        } else {
            return (mClient.addTorrent(null, mUri.toString(), false, -1, -1, null, null, null, null, null));
        }
    }

}
